/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.service.impl;

import com.liu.modules.business.domain.NovelGeneralCategory;
import com.liu.utils.*;
import lombok.RequiredArgsConstructor;
import com.liu.modules.business.repository.NovelGeneralCategoryRepository;
import com.liu.modules.business.service.NovelGeneralCategoryService;
import com.liu.modules.business.service.dto.NovelGeneralCategoryDto;
import com.liu.modules.business.service.dto.NovelGeneralCategoryQueryCriteria;
import com.liu.modules.business.service.mapstruct.NovelGeneralCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Liu
* @date 2022-07-03
**/
@Service
@RequiredArgsConstructor
public class NovelGeneralCategoryServiceImpl implements NovelGeneralCategoryService {

    private final NovelGeneralCategoryRepository novelGeneralCategoryRepository;
    private final NovelGeneralCategoryMapper novelGeneralCategoryMapper;

    @Override
    public Map<String,Object> queryAll(NovelGeneralCategoryQueryCriteria criteria, Pageable pageable){
        Page<NovelGeneralCategory> page = novelGeneralCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(novelGeneralCategoryMapper::toDto));
    }

    @Override
    public List<NovelGeneralCategoryDto> queryAll(NovelGeneralCategoryQueryCriteria criteria){
        return novelGeneralCategoryMapper.toDto(novelGeneralCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public NovelGeneralCategoryDto findById(String id) {
        NovelGeneralCategory novelGeneralCategory = novelGeneralCategoryRepository.findById(id).orElseGet(NovelGeneralCategory::new);
        ValidationUtil.isNull(novelGeneralCategory.getId(),"NovelGeneralCategory","id",id);
        return novelGeneralCategoryMapper.toDto(novelGeneralCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NovelGeneralCategoryDto create(NovelGeneralCategory resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return novelGeneralCategoryMapper.toDto(novelGeneralCategoryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NovelGeneralCategory resources) {
        NovelGeneralCategory novelGeneralCategory = novelGeneralCategoryRepository.findById(resources.getId()).orElseGet(NovelGeneralCategory::new);
        ValidationUtil.isNull( novelGeneralCategory.getId(),"NovelGeneralCategory","id",resources.getId());
        novelGeneralCategory.copy(resources);
        novelGeneralCategoryRepository.save(novelGeneralCategory);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            novelGeneralCategoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<NovelGeneralCategoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NovelGeneralCategoryDto novelGeneralCategory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("平台名", novelGeneralCategory.getPlat());
            map.put("大类名", novelGeneralCategory.getGeneralCategory());
            map.put("链接", novelGeneralCategory.getUrl());
            map.put("创建时间", novelGeneralCategory.getCreateTime());
            map.put("更新时间", novelGeneralCategory.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}